package com.mike.myfragment1;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView txt_channel;
    private TextView txt_message;
    private TextView txt_better;
    private TextView txt_setting;

    private MyFragment myFragment1, myFragment2, myFragment3, myFragment4;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentManager = getFragmentManager();
        txt_channel = findViewById(R.id.txt_channel);
        txt_message = findViewById(R.id.txt_message);
        txt_better = findViewById(R.id.txt_better);
        txt_setting = findViewById(R.id.txt_setting);
        txt_channel.setOnClickListener(this);
        txt_message.setOnClickListener(this);
        txt_better.setOnClickListener(this);
        txt_setting.setOnClickListener(this);
        txt_channel.performClick();

    }


    private void setSelected() {
        txt_channel.setSelected(false);
        txt_message.setSelected(false);
        txt_better.setSelected(false);
        txt_setting.setSelected(false);
    }

    private void hideAllFragment(FragmentTransaction fragmentTransaction) {
        if (myFragment1 != null) fragmentTransaction.hide(myFragment1);
        if (myFragment2 != null) fragmentTransaction.hide(myFragment2);
        if (myFragment3 != null) fragmentTransaction.hide(myFragment3);
        if (myFragment4 != null) fragmentTransaction.hide(myFragment4);
    }

    @Override
    public void onClick(View v) {
        FragmentTransaction fTransaction = fragmentManager.beginTransaction();
        hideAllFragment(fTransaction);
        int id = v.getId();
        if (id == R.id.txt_channel) {
            if (myFragment1 == null) {
                myFragment1 = new MyFragment("第一个Fragment");
                fTransaction.add(R.id.ly_content, myFragment1);
            } else {
                fTransaction.show(myFragment1);
            }
        }
        if (id == R.id.txt_message) {


            setSelected();
            txt_message.setSelected(true);
            if (myFragment2 == null) {
                myFragment2 = new MyFragment("第二个Fragment");
                fTransaction.add(R.id.ly_content, myFragment2);
            } else {
                fTransaction.show(myFragment2);
            }
        }
        if (id == R.id.txt_better) {
            setSelected();
            txt_better.setSelected(true);
            if (myFragment3 == null) {
                myFragment3 = new MyFragment("第三个Fragment");
                fTransaction.add(R.id.ly_content, myFragment3);
            } else {
                fTransaction.show(myFragment3);
            }
        }
        if (id == R.id.txt_setting) {
            setSelected();
            txt_setting.setSelected(true);
            if (myFragment4 == null) {
                myFragment4 = new MyFragment("第四个Fragment");
                fTransaction.add(R.id.ly_content, myFragment4);
            } else {
                fTransaction.show(myFragment4);
            }
        }
        fTransaction.commit();

    }
}